<template>
  <div class="table_con">
    <el-row>
      <tableComponents
        :isShow="false"
        @toAdd="addForm"
        :isHaveExport="false"
        :isHavePagination="false"
        :isShowBottom="false"
      >
        <template v-slot:table>
          <el-table
            :data="data"
            v-loading="loading"
            style="width: 100%;margin-bottom: 20px;"
            border
            row-key="id"
          >
            <el-table-column label="分类名称" min-width="180">
              <template slot-scope="scope">
                <div class="tree_yiji">
                  {{scope.row.title}}
                  <div class="three_title">{{scope.row.three}}级栏目(下属分类{{scope.row.length}}个)</div>
                </div>
              </template>
            </el-table-column>

            <el-table-column label="排序" align="center" width="100">
              <template slot-scope="scope">
                <div>
                  <el-input
                    @change="sortNew(scope.row)"
                    v-model.number="scope.row.sort"
                    autocomplete="off"
                    size="small"
                  ></el-input>
                </div>
              </template>
            </el-table-column>

            <el-table-column label="图片" min-width="100" align="center">
              <template slot-scope="scope">
                <div class="img_bor">
                  <img :src="$fnc.getImgUrl(scope.row.piclink)" alt />
                </div>
              </template>
            </el-table-column>

            <el-table-column label="是否显示" min-width="80">
              <template slot-scope="scope">
                <el-switch
                  class="zdySwith"
                  :width="80"
                  v-model="scope.row.show"
                  active-color="#13ce66"
                  inactive-color="#999999"
                  active-text="显示"
                  inactive-text="不显示"
                  active-value="1"
                  @change="change_show(scope.row)"
                  inactive-value="0"
                ></el-switch>
              </template>
            </el-table-column>

            <el-table-column label="创建时间" min-width="100" align="center">
              <template slot-scope="scope">{{scope.row.created_time | getTimeFormat}}</template>
            </el-table-column>

            <el-table-column label="操作" min-width="100" align="center">
              <template slot-scope="scope">
                <el-row class="myblank">
                  <div style="padding-left:20px">
                    <el-button
                      :loading="$store.state.isLoading"
                      size="mini"
                      @click="getCjTx(scope.row)"
                      v-if="scope.row.url1"
                      type="primary"
                    >采集腾讯</el-button>
                    <el-button
                      :loading="$store.state.isLoading"
                      size="mini"
                      @click="getCjAqy(scope.row)"
                      v-if="scope.row.url2"
                      type="primary"
                    >采集爱奇艺</el-button>
                    <el-button
                      :loading="$store.state.isLoading"
                      type="primary"
                      size="mini"
                      @click="editForm(scope.row)"
                    >编辑</el-button>
                    <el-button
                      :loading="$store.state.isLoading"
                      v-if="scope.row.three!='一'"
                      size="mini"
                      @click="deleteCate(scope.row.id)"
                      type="danger"
                    >删除</el-button>
                  </div>
                </el-row>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </tableComponents>
    </el-row>
    <el-dialog
      :close-on-click-modal="false"
      :title="form.id?'编辑':'添加'"
      :modal-append-to-body="false"
      :visible.sync="showform"
      class="tl size_13"
      width="800px"
    >
      <el-form :model="form" class="mar_top" label-width="100px">
        <el-form-item label="标题">
          <el-col :span="12">
            <el-input v-model="form.title" clearable></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="上级栏目">
          <el-col :span="12" class="tl">
            <el-cascader
              :options="option"
              :props="optionProps"
              :show-all-levels="false"
              v-model="op"
              change-on-select
              style="width:100%"
              :placeholder="placeText"
              @change="handleItemChange"
            ></el-cascader>
          </el-col>
        </el-form-item>

        <el-form-item label="图片地址">
          <el-col :span="12" class="tl">
            <el-input v-model="form.piclink" autocomplete="off" clearable class="mar_bottom_5"></el-input>
            <uploadpic
              :piclink="form.piclink"
              :width="150"
              :height="150"
              :is_more="false"
              @send_pic="onSuccess"
            ></uploadpic>
          </el-col>
        </el-form-item>

        <el-form-item label="腾讯网址">
          <el-col :span="12">
            <el-input v-model="form.url1" clearable></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="爱奇艺网址">
          <el-col :span="12">
            <el-input v-model="form.url2" clearable></el-input>
          </el-col>
        </el-form-item>

        <div class="mar_bottom_15">
          <span class="sizeColor size_13 sp_style"></span>
          <el-button
            :loading="$store.state.isLoading"
            size="medium"
            type="primary"
            @click.prevent="subform"
          >确定</el-button>
        </div>
      </el-form>
    </el-dialog>

    <el-dialog
      :close-on-click-modal="false"
      title="采集"
      :modal-append-to-body="false"
      :visible.sync="showCj"
      class="size_13"
      width="500px"
    >
      <el-row class="progress_div">
        <!-- <el-col :span="24" class="mar_bottom_15">
          <el-progress type="circle" :percentage="authProgress"></el-progress>
        </el-col>-->
        <el-col :span="24" class="mar_bottom_25">第{{authProgress}}页采集中，请等待...</el-col>
        <el-col :span="24">
          <el-button
            :loading="$store.state.isLoading"
            size="medium"
            type="primary"
            @click.prevent="showCj = false"
          >暂停采集</el-button>
          <el-button
            :loading="$store.state.isLoading"
            v-if="timeOut"
            size="medium"
            type="primary"
            @click.prevent="jxCj"
          >继续采集</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import uploadpic from "@/components/page/pic/up_pic";
import axios from "axios";
import qs from "qs";
var md5 = require("md5");
export default {
  data() {
    return {
      data: [],
      op: [],
      option: [],
      optionProps: {
        value: "id",
        label: "title",
        children: "z"
      },
      form: {},
      showform: false,
      loading: false,
      showCj: false,
      placeText: "请选择栏目添加",
      cjObj: {},
      authProgress: 1,
      timeOut: false,
      params: {
        page: 1,
        id: 0,
        types: 0
      }
    };
  },
  watch: {},
  computed: {},
  created() {
    this.getCateList();
  },
  methods: {
    jxCj() {
      this.timeOut = false;
      this.getCjAll();
    },
    getCjAll() {
      if (this.showCj != true) {
        return;
      }
      var params = {};
      params = JSON.parse(JSON.stringify(this.params)) || {};
      var uid = localStorage.getItem("adminuid") || "";
      var time = parseInt(new Date().getTime() / 1000) + "";
      var admin_key = localStorage.getItem("admin_key") || "";
      var str = qs.stringify(params);
      var url = "";
      if (str == "") {
        url =
          "admin_key=" +
          admin_key +
          "&headuid=" +
          uid +
          "&timestamp=" +
          time +
          "&meid=" +
          "" +
          "&extra=" +
          "";
      } else {
        url =
          str +
          "&admin_key=" +
          admin_key +
          "&headuid=" +
          uid +
          "&timestamp=" +
          time +
          "&meid=" +
          "" +
          "&extra=" +
          "";
      }
      var arr = url.split("&");
      for (var i in arr) {
        if (arr[i].indexOf("%") >= 0) {
          arr[i] = decodeURIComponent(arr[i]);
        }
      }
      var urlStr = arr.sort().join("&");
      params.sign = md5("@" + urlStr + "@");
      axios
        .post("/api/admin/movies_cate/cj", qs.stringify(params), {
          headers: {
            headuid: uid,
            timestamp: time,
            meid: "",
            extra: ""
          },
          timeout: 180000
        })
        .then(res => {
          if (res.data.code == 200) {
            if (!isNaN(res.data.result) && res.data.result != false) {
              this.params.page = Number(res.data.result) + 1;
              this.authProgress = Number(res.data.result) + 1;
              this.getCjAll();
            } else {
              this.showCj = false;
              this.$fnc.alertSuccess({ text: "采集完成" });
            }
          } else {
            this.timeOut = true;
            this.$fnc.alertError(res.data.code_desc, res.data.result);
          }
        });
    },
    getCjTx(item) {
      if (item.url1) {
        this.showCj = true;
        this.params = {
          id: item.id,
          types: 1,
          page: 1
        };
        this.authProgress = 1;
        this.getCjAll();
      }
    },
    getCjAqy(item) {
      if (item.url2) {
        this.showCj = true;
        this.params = {
          id: item.id,
          types: 2,
          page: 1
        };
        this.authProgress = 1;
        this.getCjAll();
      }
    },
    onSuccess(pic) {
      this.form.piclink = pic;
    },
    handleItemChange(val) {
      this.form.parent_id = val[val.length - 1];
    },
    subform() {
      if (this.form.parent_id == 0 || this.form.parent_id == "") {
        this.$fnc.alertError("请选择栏目添加");
        return;
      }
      if (this.form.id) {
        this.moviesCateEdit(this.form);
      } else {
        this.$api.getMovies.movies_cate_saveadd(this.form).then(res => {
          if (res.data.code == 200) {
            this.$fnc.alertSuccess("操作成功");
            this.showform = false;
            this.getCateList();
          } else {
            this.$fnc.alertError(res.data.result);
          }
        });
      }
    },
    sortNew(row) {
      delete row.z;
      delete row.children;
      this.moviesCateEdit(row);
    },
    moviesCateEdit(data) {
      var params = {};
      params = data || {};
      this.$api.getMovies.movies_cate_saveedit(params).then(res => {
        if (res.data.code == 200) {
          this.showform = false;
          this.getCateList();
        } else {
          this.$fnc.alertError(res.data.result);
        }
      });
    },
    addForm() {
      this.form = {
        parent_id: "0",
        title: "",
        piclink: ""
      };
      this.showform = true;
    },
    editForm(item) {
      this.op = [];
      this.$api.getMovies.movies_cate_edit({ id: item.id }).then(res => {
        if (res.data.code == 200) {
          this.form = res.data.result;
          this.op = res.data.result.up_id.split("@");
          this.showform = true;
        }
      });
    },
    deleteCate(id) {
      this.$swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        if (willDelete.value) {
          var data = { id: id };
          this.$api.getMovies.movies_cate_del(data).then(res => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              this.getCateList();
            } else {
              this.$swal("删除失败！", res.data.result, "error");
            }
          });
        }
      });
    },
    getCateList() {
      this.loading = true;
      var params = {};
      params.parent_id = "";
      this.$api.getMovies.movies_cate_lists(params).then(res => {
        if (res.data.code == 200 && res.data.status == 1 && res.data.result) {
          this.option = res.data.result;
          this.data = res.data.result.filter(item => {
            if (item.z) {
              item.title = item.title;
              item.length = item.z.length;
              item.z = item.z.filter(it => {
                if (it.z) {
                  it.title = it.title;
                  it.length = it.z.length;
                  it.z = it.z.filter(t => {
                    if (t.z) {
                      t.title = t.title;
                      t.length = t.z.length;
                      t.children = t.z;
                    } else {
                      t.length = 0;
                    }
                    t.three = "三";
                    t.index = 2;
                    return t;
                  });
                  it.children = it.z;
                } else {
                  it.length = 0;
                }
                it.three = "二";
                it.index = 1;
                return it;
              });
              item.children = item.z;
              item.three = "一";
              return item;
            } else {
              item.length = 0;
              item.three = "一";
              item.index = 0;
              return item;
            }
          });
        }
        this.loading = false;
      });
    }
  },
  components: {
    uploadpic
  }
};
</script>
<style  scope>
.progress_div {
  width: 100%;
  text-align: center;
}
</style>
